package com.ycy.plugin;

/**
 * 对称加密
 */
public class SymmetricAlgorithmFactory {

  public static SymmetricAlgorithm getSymmetricAlgorithm(String secretType, String secret, String iv, String out, String toCase) {
    if (secretType == null || secret == null) {
      throw new RuntimeException("secretType or secret 未指定");
    }
    if (secretType.startsWith("AES")) {
      return new AESAlgorithm(secretType, secret, iv, out, toCase);
    }
    if (secretType.startsWith("DES")) {
      return new DESAlgorithm(secretType, secret, iv, out, toCase);
    }
    throw new RuntimeException("not support " + secretType);
  }

  public static void main(String[] args) {
    SymmetricAlgorithm symmetricAlgorithm = SymmetricAlgorithmFactory.getSymmetricAlgorithm("AES/CBC/PKCS5Padding", "1234567890abcdef", "1234567890abcdef", null, null);
    String data = "[{" +
            "\"zhihui_devcie_socialcode\":\"91331002735278711B\",\n" +
            "\"zhihui_device_lat\":\"28.673909\",\n" +
            "\"zhihui_device_lng\":\"121.472395\",\n" +
            "\"zhihui_device_name\":\"1号进线计量柜（拨号1）\",\n" +
            "\"zhihui_device_mac\":\"317700300201\",\n" +
            "\"zhihui_device_pid\":\"1\"\n" +
            "}]";

    String encrypt = null;
    try {
      encrypt = symmetricAlgorithm.encrypt(data);
      System.out.println(encrypt);
      System.out.println(symmetricAlgorithm.decrypt(encrypt));
    } catch (Exception e) {
      e.printStackTrace();
    }


    try {
      SymmetricAlgorithm zsduioty = SymmetricAlgorithmFactory.getSymmetricAlgorithm("DES/CBC/PKCS5Padding", "zsduioty", "32178694", null, null);
      String encrypt1 = zsduioty.encrypt(data);
      //231e93035dd326aad514b8d7d12c3d86
      System.out.println(encrypt1);

      String decrypt = zsduioty.decrypt(encrypt1);
      System.out.println(decrypt);

    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println();

  }

}
